import {hrAjax as proFetch, saveValidate} from 'src/hrpub/common/utils/utils';
import {toast} from 'nc-lightapp-front';

export default function saveData(step = '0') {
    const {
        lastAgreementPage, lastContractPage, tabKey, orgVal, contractArea, contractSubArea = [],
        agreeArea, agreeSubArea = [], contractData, agreeData, json, contractPageid, agreePageid
    } = this.state;
    if (tabKey === 'contract') {
        if (!this.props.form.isCheckNow(contractArea)) return;
        saveValidate(this.props, contractPageid, contractArea, contractSubArea, 'extcard').then(() => {
            proFetch({
                url: '/nccloud/hrcm/account/SaveAccountAction.do',
                body: {
                    pk_org: orgVal.refpk,
                    step: step,
                    account: {
                        model: {
                            rows: this.props.form.getAllFormValue(contractArea).rows
                        }
                    },
                    isContract: true
                },
            }).then((res) => {
                if (res.success) {
                    if (res.data.step !== '99') {
                        this.alertModalNode.show([res.data.msg], '',
                            () => {
                                saveData.call(this, res.data.step)
                            })
                    } else {
                        toast({color: "success", content: json['cm6011-000005']});
                        if (lastContractPage === 'view') {
                            this.props.form.setFormStatus(contractArea, 'browse');
                            this.viewContract('', contractData.rows[0].values)
                        } else {
                            this.props.form.setFormStatus(contractArea, 'browse');
                            this.setState({
                                contractPage: lastContractPage,
                                contractPageInfo: {
                                    ...this.state.contractPageInfo,
                                    pageIndex: 1
                                }
                            }, () => {
                                this.updateButtonStatus();
                                this.getConTableData(false);
                            });
                        }
                    }
                }
            });
        })
    } else {
        if (!this.props.form.isCheckNow(agreeArea)) return;
        saveValidate(this.props, agreePageid, agreeArea, agreeSubArea, 'extcard').then(() => {
            proFetch({
                url: '/nccloud/hrcm/account/SaveAccountAction.do',
                body: {
                    pk_org: orgVal.refpk,
                    step: step,
                    account: {
                        model: {
                            rows: this.props.form.getAllFormValue(agreeArea).rows
                        }
                    },
                    isContract: false
                },
            }).then((res) => {
                if (res.success) {
                    if (res.data.step !== '99') {
                        this.alertModalNode.show([res.data.msg], '',
                            () => {
                                saveData.call(this, res.data.step)
                            })
                    } else {
                        toast({color: "success", content: json['cm6011-000005']});
                        if (lastAgreementPage === 'view') {
                            this.props.form.setFormStatus(agreeArea, 'browse');
                            this.viewAgree('', agreeData.rows[0].values)
                        } else {
                            this.props.form.setFormStatus(agreeArea, 'browse');
                            this.setState({
                                agreementPage: lastAgreementPage,
                                agreementPageInfo: {
                                    ...this.state.agreementPageInfo,
                                    pageIndex: 1
                                }
                            }, () => {
                                if (lastAgreementPage === 'main') {
                                    this.updateButtonStatus();
                                    this.getAgreeTableData(this.state.isQueryTemplate, false);
                                } else {
                                    this.updateButtonStatus();
                                }
                            });
                        }
                    }
                }
            });
        })
    }
}
